﻿using System;
using System.Collections.Generic;
using Bricks.Injecting;

namespace Bricks.Validation
{

    /// <summary>
    /// 
    /// </summary>
    public static class Validation
    {

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TTarget"></typeparam>
        /// <param name="target"></param>
        /// <returns></returns>
        public static IEnumerable<ValidationResult> Validate(object target)
        {
            return Validate(ResolveValidatorFacadeFactory(), target);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TTarget"></typeparam>
        /// <param name="key"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static IEnumerable<ValidationResult> Validate(string key, object target)
        {
            return Validate(ResolveValidatorFacadeFactory(key), target);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TTarget"></typeparam>
        /// <param name="target"></param>
        /// <returns></returns>
        public static IEnumerable<ValidationResult> Validate<TTarget>(TTarget target)
        {
            return Validate<TTarget>(ResolveValidatorFacadeFactory(), target);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TTarget"></typeparam>
        /// <param name="key"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static IEnumerable<ValidationResult> Validate<TTarget>(string key, TTarget target)
        {
            return Validate<TTarget>(ResolveValidatorFacadeFactory(key), target);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="validatorFacade"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        private static IEnumerable<ValidationResult> Validate(IValidatorFacadeFactory validatorFacadeFactory, object target)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            IValidatorFacade validatorFacade = validatorFacadeFactory.Create(target.GetType());

            return validatorFacade.Validate(target);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TTarget"></typeparam>
        /// <param name="validatorFacadeFactory"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        private static IEnumerable<ValidationResult> Validate<TTarget>(IValidatorFacadeFactory validatorFacadeFactory, TTarget target)
        {
            IValidatorFacade<TTarget> validatorFacade = validatorFacadeFactory.Create<TTarget>();

            return validatorFacade.Validate(target);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static IValidatorFacadeFactory ResolveValidatorFacadeFactory()
        {
            IValueResolver<IValidatorFacadeFactory> factoryResolver = new SingleValueResolver<IValidatorFacadeFactory>();

            return factoryResolver.Resolve();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private static IValidatorFacadeFactory ResolveValidatorFacadeFactory(string key)
        {
            IValueResolver<IValidatorFacadeFactory> factoryResolver = new SingleValueResolver<IValidatorFacadeFactory>(key);

            return factoryResolver.Resolve();
        }
    }
}
